<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script type="text/javascript" src="/resources/easyui/jquery.min.js"/>
    <link rel="stylesheet" type="text/css" href="/resources/easyui/themes/insdep/easyui.css">
    <link rel="stylesheet" type="text/css" href="/resources/easyui/themes/insdep/easyui_plus.css">
    <link rel="stylesheet" type="text/css" href="/resources/easyui/themes/insdep/easyui_animation.css">
    <link rel="stylesheet" type="text/css" href="/resources/easyui/themes/insdep/insdep_theme_default.css">
    <link rel="stylesheet" type="text/css" href="/resources/easyui/themes/insdep/icon.css">
    <script type="text/javascript" src="/resources/easyui/jquery.easyui.min.js"/>
    <script type="text/javascript" src="/resources/easyui/themes/insdep/jquery.insdep-extend.min.js"/>
    <script type="text/javascript" src="/resources/easyui/locale/easyui-lang-zh_CN.js"/>
    <script src="/resources/utils/util.js"></script>
    <title>Title</title>

    <script>
        var datagrid;
        $(function () {
            datagrid=$("#userId").datagrid({
                url:"/page/SelectUserAll",
                method:"post",
                pagination:true,//分页插件
                pageSize:10,
                pageList:[10,20,30,40],//表示分页页码列表
                fit:true,//布局选项 当设置为 true 时，就设置布局（layout）的尺寸适应它的父容器。当在 'body' 标签上创建布局（layout）时，它将自动最大化到整个页面的全部尺寸。
                fitColumns:true, //使列自动展开/收缩列的宽度以适应网格的宽度，防止水平滚动。,fit不是datagrid的属性，是panel的属性，panel大小将自适应父容器
                columns:[
                    [
                    {field:'id',checkbox:true,},
                    {title:"账号",field:'name',width:100},
                    {title:'密码',field:'password',width:100},
                    {title:'姓名',field:'orders',width:100},
                    {title:'邮箱',field:'email',width:100},
                    {title:'信息',field:'message',width:100},
                    {title:"创建时间",field:'createdate',width:100,formatter:function (value,row) {
                            return sdf(value);
                        }}
                ]
                ]
            });

            //修改
        $("#edit").click(function () {
            var data=datagrid.datagrid('getSelections');//获取选中的数据----选中多行
            if(data.length==1){
                var row=datagrid.datagrid('getSelected');//获取选中的数据----选中1行
                var dialog=$("info_dialog").dialog({
                    title:'查看用户',width: 500, height: 300, iconCls: 'icon-edit', href: '/page/edit.html', modal: true,
                    onLoad:function () {
                        $("#editForm #name").textbox('setValue',row.name);
                        $("#editForm #orders").textbox('setValue',row.orders);
                        $("#editForm #email").textbox('setValue',row.email);
                        $("#editForm #message").textbox('setValue',row.message);
                    },
                    buttons:[{text:'修改',iconCls:'icon-save',handler:function () {
                            console.log(getFormJson("editForm"));
                            if (validateText($("#editForm input[name=name]"))
                                && validateText($("#editForm input[name=password]"))) {
                                $.ajax({
                                    url: getRootPath() + '/page/edit',
                                    type: 'post',
                                    data: getFormJson("editForm"),
                                    dataType: 'json',
                                    success: function (data) {
                                        if (data['code'] == "200") {
                                            $.messager.alert("提示!", data["msg"], "info");
                                            dialog.dialog('close');
                                            clearCheckGrid(datagrid);
                                            loadData();
                                        } else {
                                            $.messager.alert("提示!", "超时", "warning");
                                        }
                                    }
                                });
                            } else {
                                alert("账号密码不能为中文");
                            }
                        }
                        },
                        {text:'取消',iconCls:'icon-cancel',
                            handler:function () {
                                dialog.dialog('close');
                        }}]
                });
            }else if(data.length>1){
                $.messager.alert("提示","最多选取一行","error");
            }else{
                $.messager.alert("提示","至少选取一行","info");
            }
        });

        //查看
            $("#view").click(function () {
                var data=datagrid.dialog("getSelections");
                if(data.length==1){
                    var row=datagrid.datagrid("getSelected");
                    var dialog=$("#info_dialog").dialog({
                        title:"查看用户",width:500,height:300,iconCls:'icon-search',href:'/page/view.html',modal:true,
                        onLoad:function () {
                            $("#viewtable #name").textbox('setValue',row.name);
                            $("#viewtable createdate").textbox('setValue',sdf(row.createdate));
                            $("#viewtable #orders").textbox('setValue',row.orders);
                            $("#viewtable #email").textbox('setValue',row.email);
                            $("#viewtable #message").textbox('setValue',row.message);
                        },
                        buttons:[{text:'关闭',iconCls:'icon-cancel',
                        handler:function () {
                            dialog.dialog('close');
                        }}]
                    });
                }else if(data.length>1){
                    $.messager.alert("提示","最多选一行","error");
                }else{
                    $.messager.alert("提示","至少选中一行","info");
                }
            });

            //删除
            $("#delete").click(function () {
                var data=datagrid.dialog("getSelections");
                if(data.length>=1){
                    var ids=getGridIds(datagrid,data);
                    $.messager.confirm("操作提示","确定要删除吗？",function (data) {
                        if(data){
                            $.get(getRootPath()+"/page/delete",{'ids[]':ids},
                                function (res,status,xhr) {
                                if(res.code=="200"){
                                    $.messager.alert("提示!",res["msg"],"info");
                                    clearCheckGrid(datagrid);//删除id
                                    loadData();
                                }else{
                                    $.messager.alert("提示","超时","warning");
                                }
                            });
                        }
                    });
                }else{
                    $.messager.alert("提示","至少选择一行","warning");
                }
            });

            //添加
            $("#add").click(function () {
                var dialog=$("#info_dialog").dialog({
                    title:"添加信息",width: 500,height: 300,iconCls: 'icon-add', modal: true,href:'/page/add.html',
                    buttons:[{
                        text:"保存",
                        iconCls:"icon-ok",
                        handler:function () {
                            if ($("#addForm").form('validate')) {
                                if (validateText($("#addForm input[name=name]"))
                                    && validateText($("#addForm input[name=password]"))) {
                                    $.ajax({
                                        url: getRootPath() + "/page/add",
                                        type: 'post',
                                        data: getFormJson("addForm"),
                                        dataType: "json",
                                        success: function (data) {
                                            console.info(data);
                                            if (data["code"] == "200") {
                                                $.messager.alert("提示", data["msg"], "info");
                                                dialog.dialog("close");
                                                clearCheckGrid(datagrid);
                                                loadData();
                                            } else {
                                                $.messager.alert("提示", data["msg"], "warning");
                                            }

                                        }
                                    });
                                }
                            } else {
                                alert("账号密码不能有中文");
                                return;
                            }
                        }
                        },{
                            text:"关闭",
                        iconCls:'icon-cancel',
                        handler:function () {
                            dialog.dialog('close');
                        }}]
                });
            });
        })
    </script>
</head>
<body>
<div region="center" border="false">
    <table id="userId" border="false" toolbar="#toolbar"></table>
</div>
</body>
</html>